const rules = {
    req: (rulesItem) => {
        return {
            required: true,
            message: rulesItem.msg || '请输入',
            trigger: rulesItem.tri || 'blur'
        }
    },
    len: (rulesItem) => {
        let item = {
            message: rulesItem.msg || '不符合长度限制',
            trigger: rulesItem.tri || 'blur'
        }
        if (rulesItem.min) {
            item.min = rulesItem.min
        }
        if (rulesItem.max) {
            item.max = rulesItem.max
        }
        return item
    },
    phone: (rulesItem) => {
        return {
            pattern: /^1[23456789]\d{9}$/,
            message: rulesItem.msg || '电话格式不正确',
            trigger: rulesItem.tri || 'blur'
        }
    },
    email: (rulesItem) => {
        return {
            pattern: /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/,
            message: rulesItem.msg || '邮箱格式不正确',
            trigger: rulesItem.tri || 'blur'
        }
    },
    idCard: (rulesItem) => {
        return {
            pattern: /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/,
            message: rulesItem.msg || '身份证格式不正确',
            trigger: rulesItem.tri || 'blur'
        }
    },
    landline: (rulesItem) => {
        return {
            pattern: /^\d{3,4}-\d{7,8}(-\d{3,4})?$/,
            message: rulesItem.msg || '固定电话格式不正确',
            trigger: rulesItem.tri || 'blur'
        }
    },
    reg: (rulesItem) => {
        return {
            pattern: rulesItem.reg,
            message: rulesItem.msg || '格式不正确',
            trigger: rulesItem.tri || 'blur'
        }
    },
    custom: (rulesItem) => {
        return {
            validator: rulesItem.met,
            message: rulesItem.msg,
            trigger: rulesItem.tri || 'blur'
        }
    },
    num: (rulesItem) => {
        return {
            validator: (rule, value, callback) => setNumCustomize(rule, value, callback, rulesItem),
            message: rulesItem.msg,
            trigger: rulesItem.tri || 'blur'
        }
    },
}

/**
 * 设置数字验证规则
 * @param rule
 * @param value
 * @param callback
 * @param rulesItem
 */
function setNumCustomize(rule, value, callback, rulesItem) {
    let valueNum = Number(value)
    if (isNaN(valueNum)) {
        callback('请输入数字类型')
    }
    if (rulesItem.max && valueNum > rulesItem.max) {
        callback(`输入不能大于${rulesItem.max}`)
    }
    if (rulesItem.min && valueNum < rulesItem.min) {
        callback(`输入不能小于${rulesItem.min}`)
    }
    callback()
}

export default rules